package wangshuai.risk.action;

import java.util.Map;

import hejie.admin.dao.AdminDao;
import hejie.admin.dao.EmployeeDao;
import hejie.admin.service.AdminService;
import hejie.admin.service.EmployeeService;

import javax.annotation.Resource;
import javax.crypto.spec.PSource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.junit.runner.Request;

import publics.model.Admin;
import publics.model.Employee;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletResponseAware {
	
	private HttpServletResponse servletResponse;
	
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	private AdminDao adminDao;
	private EmployeeDao employeeDao;
	
	public AdminDao getAdminDao() {
		return adminDao;
	}
	@Resource(name="adminDao")
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}	
	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}
	@Resource(name="employeeDao")
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	private String userName;
	private String passWord;
	private String validateNo;
	private Employee employee;
	private Admin admin;
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		if(userName == null || passWord == null){
			return LOGIN;
		}
		servletResponse.setCharacterEncoding("UTF-8");
		this.setUserName(this.getUserName().trim()); //去除 帐号 密码 首尾空格
		this.setPassWord(this.getPassWord().trim());
		System.out.println("userName="+userName+", passWord="+passWord+", validateNo="+validateNo);
		
		//验证验证码是否填写正确
		String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(kaptchaExpected==null || !kaptchaExpected.equals(validateNo)){//填写错误
			System.out.println("验证码填写错误 ");
			servletResponse.getWriter().print("validate_error");
			return null;
		}
		employee = this.getEmployeeDao().findByUserAndPass(userName, passWord);
		admin = this.getAdminDao().findByUserAndPass(userName, passWord);
		if((employee == null) && (admin == null)){ //未找到
			System.out.println("用户名或密码错误");
			servletResponse.getWriter().print("user_error");
			
			return null;
		}
		Map map = ActionContext.getContext().getSession();
		if(employee != null){
			System.out.println("Employee="+employee);
			
			map.put("user", employee.getLoginUser());
			map.put("level", employee.getRight().getId());
			map.put("name", employee.getName());
			map.put("id", employee.getId());
			map.put("leftPage", this.findLeftPage(employee.getRight().getId())); //填入左侧导航栏
			
			this.getEmployee().setLoginPass(null);//防止密码泄漏
			//添加结果处理代码
			servletResponse.getWriter().print("succIn.action");
			System.out.println("成功登录e！");
			return null;
			
		}
		if(admin != null){
			System.out.println("Admin="+admin);
			
			map.put("user", admin.getUser());
			map.put("level", admin.getRight().getId());
			map.put("name", admin.getName());
			map.put("id", admin.getId());
			map.put("leftPage",this.findLeftPage(admin.getRight().getId()));//填入左侧导航栏
			
			this.getAdmin().setPass(null);//防止密码泄漏
			
			servletResponse.getWriter().print("succIn.action");
			System.out.println("成功登录a！");
			return null;
		}
		
		return LOGIN;
	}
	
	
	/**
	 * 根据权限确定返回的左侧导航栏
	 * @param level  //权限
	 * @return
	 */
	public String findLeftPage(Integer level) {
		
		if(level == null){
			System.out.println("权限为空,登录失败：level="+level);
			return null;
		}
		if(level.equals(1) || level.equals(9)){//黑名单和未认证，不能登录
			return null;
		}else if (level.equals(2)) {	//已认证客户（企业）
			return "leftPage/leftTwo.html";
		}else if(level.equals(3)){		//客户经理（基本信息录入）
			return "leftPage/leftThree.html";
		}else if(level.equals(4)){		//风险经理（初审）
			return "leftPage/leftFour.html";
		}else if(level.equals(5)){		//二审人员
			return "leftPage/leftFive.html";
		}else if(level.equals(6)){		//终审人员
			return "leftPage/leftSix.html";
		}else if(level.equals(7)){		//银行系统管理员
			return "leftPage/leftSeven.html";
		}else if(level.equals(8)){		//顶级系统管理员
			return "leftPage/leftF.html";
		}
		
		return null;
	}
	
	
	

	/**
	 * 退出登录
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String logout() throws Exception{
		Map map = ActionContext.getContext().getSession();
		System.out.println("退出前 user="+map.get("user")+", level="+map.get("level")+", name="+map.get("name"));
		
		map.remove("user");
		map.remove("level");
		map.remove("name");
		map.remove("id");
		
		System.out.println("退出后 user="+map.get("user")+", level="+map.get("level")+", name="+map.get("name"));
		return SUCCESS;
	}
	
	
	
	
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @return the passWord
	 */
	public String getPassWord() {
		return passWord;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @param passWord the passWord to set
	 */
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	/**
	 * @return the employee
	 */
	public Employee getEmployee() {
		return employee;
	}
	/**
	 * @return the admin
	 */
	public Admin getAdmin() {
		return admin;
	}
	/**
	 * @param employee the employee to set
	 */
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	/**
	 * @param admin the admin to set
	 */
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	/**
	 * @return the validateNo
	 */
	public String getValidateNo() {
		return validateNo;
	}
	/**
	 * @param validateNo the validateNo to set
	 */
	public void setValidateNo(String validateNo) {
		this.validateNo = validateNo;
	}

	

}
